function formatMoney(num) {
  num = new String(num)
  num = num.toString().replace(/\$|\,/g, '')
  if (isNaN(num)) { num = '0' }
  sign = (num == (num = Math.abs(num)))
  num = Math.floor(num * 100 + 0.50000000001)
  cents = num % 100
  num = Math.floor(num / 100).toString()
  if (cents < 10) { cents = '0' + cents }
  for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
    num = num.substring(0, num.length - (4 * i + 3)) + ',' +
      num.substring(num.length - (4 * i + 3))
  }
  return (((sign) ? '' : '-') + num + '.' + cents)
}

function formatNumber(number, decimals, dec_point, thousands_sep) {
  /*
  * 参数说明：
  * number：要格式化的数字
  * decimals：保留几位小数
  * dec_point：小数点符号
  * thousands_sep：千分位符号
  * */
  number = (number + '').replace(/[^0-9+-Ee.]/g, '')
  var n = !isFinite(+number) ? 0 : +number
  var prec = !isFinite(+decimals) ? 0 : Math.abs(decimals)
  var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep
  var dec = (typeof dec_point === 'undefined') ? '.' : dec_point
  var s = ''
  var toFixedFix = function(n, prec) {
    var k = Math.pow(10, prec)
    return '' + Math.ceil(n * k) / k
  }

  s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.')
  var re = /(-?\d+)(\d{3})/
  while (re.test(s[0])) {
    s[0] = s[0].replace(re, '$1' + sep + '$2')
  }

  if ((s[1] || '').length < prec) {
    s[1] = s[1] || ''
    s[1] += new Array(prec - s[1].length + 1).join('0')
  }
  return s.join(dec)
}

Vue.filter('formatMoneyFilter', function(value) {
  return formatMoney(value)
})

Vue.filter('formatNumberFilter', function(value, decimal) {
  if (decimal == null) { decimal = 0 }

  return formatNumber(value, decimal, '.', ',')
})

Vue.filter('formatDateFilter', function(value, formatString) {
  if (value == null) { return '' }
  formatString = formatString || 'YYYY-MM-DD'
  return moment(value).format(formatString)
})
